package com.seo4j.adb4j.gui;

import com.seo4j.adb4j.device.listener.IAndroidDevice;
import com.seo4j.adb4j.gui.console.EmulatorlCommandHandler;
import com.seo4j.adb4j.gui.console.PoseidonConsole;
import com.seo4j.adb4j.gui.panel.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;


public class DeviceTabPanel extends JPanel {
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("com.seo4j.adb4j.gui.messages"); //$NON-NLS-1$

    private JTabbedPane tabbedPane;

    private HomePanel homePanel;
    private PicturePanel picturePanel;
    private FileTreePanel fileTreePanel;
    private CallLogPanel callLogPanel;
    private ContactPanel contactPanel;
    private MonitorPanel monitorCall, monitorSMS;
    //  private ColorPane userLogPanel;
    private SMSLogPanel smsPanel;

    private HashMap<JPanel, Integer> panChanMap;

    private String imei;
    private AdbGUI gui;

    public DeviceTabPanel(String imei, AdbGUI gui) {

        this.imei = imei;
        this.gui = gui;

        panChanMap = new HashMap<JPanel, Integer>();

        this.initGUI();


    }

    public void launchMessageDialog(String txt, String title, int type) {
        JOptionPane.showMessageDialog(this, txt, title, type);
    }


    public void removeTab(JPanel viewer) {

        if (viewer instanceof PicturePanel) {
            picturePanel = null;
        }
        if (viewer instanceof FileTreePanel) {
            fileTreePanel = null;
        }
        if (viewer instanceof CallLogPanel) {
            callLogPanel = null;
        }
        if (viewer instanceof SMSLogPanel) {
            smsPanel = null;
        }
        if (viewer instanceof ContactPanel) {
            contactPanel = null;
        }
        if (viewer instanceof MonitorPanel) {
            if (((MonitorPanel) viewer).getCallMonitor()) {
                //	if(monitorCall.getMonitoring()) gui.fireStopCallMonitoring(imei, panChanMap.get(monitorCall));
                monitorCall = null;
            } else {
                //		if(monitorSMS.getMonitoring()) gui.fireStopSMSMonitoring(imei, panChanMap.get(monitorSMS));
                monitorSMS = null;
            }
        }
        tabbedPane.remove(viewer);
    }


    // ********************
    // M�thodes pour home
    // ********************


    public void updatePreference(String ip, int port, boolean wait, ArrayList<String> phones, ArrayList<String> sms, ArrayList<String> kw) {
        homePanel.updatePreferences(ip, port, wait, phones, sms, kw);
    }


    // ********************
    // M�thodes pour la Map
    // ********************

    // *********************
    // M�thodes pour l'image
    // *********************

    public void updatePicture(byte[] picture) {
        if (picturePanel != null) picturePanel.updateImage(picture);
    }

    // *********************
    // M�thodes pour le son
    // *********************


    // ****************************
    // M�thodes pour la video
    // ****************************


    // ****************************
    // M�thodes pour l'arborescence
    // ****************************

    // ****************************
    // M�thodes pour les call logs
    // ****************************


    // ****************************
    // M�thodes pour les contacts
    // ****************************


    // ****************************
    // M�thodes pour monitors
    // ****************************

    public void addMonitoredCall(int type, String phoneNumber) {
        if (monitorCall != null) monitorCall.addMonitoredCall(type, phoneNumber);
    }

    public void addMonitoredSMS(String addr, long date, String body) {
        if (monitorSMS != null) monitorSMS.addMonitoredSMS(addr, date, body);
    }

    // ****************************
    // M�thodes de save channel
    // ****************************

    public void saveCallLogChannel(int channel) {
        panChanMap.put(callLogPanel, channel);
    }

    public void saveContactChannel(int channel) {
        panChanMap.put(contactPanel, channel);
    }

    public void saveMonitorSMSChannel(int channel) {
        panChanMap.put(monitorSMS, channel);
    }

    public void saveMonitorCallChannel(int channel) {
        panChanMap.put(monitorCall, channel);
    }

    public void savePictureChannel(int channel) {
        panChanMap.put(picturePanel, channel);
    }


    // ****************************
    // M�thodes des boutons UserGUI
    // ****************************

    private void fireButtonTakePicture() {
        if (picturePanel == null) {
            picturePanel = new PicturePanel(this);
            tabbedPane.addTab(BUNDLE.getString("Picture-viewer"), picturePanel);
        }
        tabbedPane.setSelectedComponent(picturePanel);
    }

    private void fireButtonFileTree() {
        if (fileTreePanel == null) {
            fileTreePanel = new FileTreePanel(this);
            tabbedPane.addTab(BUNDLE.getString("File-tree-viewer"), fileTreePanel);
        }
        tabbedPane.setSelectedComponent(fileTreePanel);
    }

    private void fireButtonCallLogs() {
        if (callLogPanel == null) {
            callLogPanel = new CallLogPanel(this);
            tabbedPane.addTab(BUNDLE.getString("Call-logs"), callLogPanel);
        }
        tabbedPane.setSelectedComponent(callLogPanel);
    }

    private void fireButtonContacts() {
        if (contactPanel == null) {
            contactPanel = new ContactPanel(this);
            tabbedPane.addTab(BUNDLE.getString("Contacts-tab"), contactPanel);
        }
        tabbedPane.setSelectedComponent(contactPanel);
    }


    private void fireButtonSMS() {
        if (smsPanel == null) {
            smsPanel = new SMSLogPanel(this);
            tabbedPane.addTab(BUNDLE.getString("SMS-viewer"), smsPanel);
        }
        tabbedPane.setSelectedComponent(smsPanel);
    }


    public void fireButtonCloseTab() {
        JPanel panel = (JPanel) tabbedPane.getSelectedComponent();
        if (panel == homePanel) {
            JOptionPane.showMessageDialog(this, BUNDLE.getString("colse-home-title"), BUNDLE.getString("Forbidden-action"), JOptionPane.ERROR_MESSAGE);
        } else {
            this.removeTab(panel);
        }
    }


    private void fireButtonMonitorCall() {
        if (monitorCall == null) {
            monitorCall = new MonitorPanel(this, true);
            tabbedPane.addTab("Call monitor", monitorCall);
        }
        tabbedPane.setSelectedComponent(monitorCall);
    }

    private void fireButtonMonitorSMS() {
        if (monitorSMS == null) {
            monitorSMS = new MonitorPanel(this, false);
            tabbedPane.addTab("SMS monitor", monitorSMS);
        }
        tabbedPane.setSelectedComponent(monitorSMS);
    }


    /**
     * Create the frame.
     * Don't touch the code !!
     */
    private void initGUI() {
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 672, 584);


        JToolBar toolBar = new JToolBar();
        toolBar.setBorderPainted(true);
        toolBar.setFloatable(false);

        JButton closeTabViewer = new JButton(BUNDLE.getString("Close-Tab")); //$NON-NLS-1$
        //  closeTabViewer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
        closeTabViewer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonCloseTab();
            }
        });
        toolBar.add(closeTabViewer);
        toolBar.addSeparator();


        JButton prendrePhoto = new JButton(BUNDLE.getString("Take-pic")); //$NON-NLS-1$
        prendrePhoto.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonTakePicture();
            }
        });
        toolBar.add(prendrePhoto);
        toolBar.addSeparator();

        JButton fileTree = new JButton(BUNDLE.getString("File-tree")); //$NON-NLS-1$

        fileTree.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonFileTree();
            }
        });
        toolBar.add(fileTree);
        toolBar.addSeparator();

        JButton contactsBtn = new JButton(BUNDLE.getString("Contacts")); //$NON-NLS-1$
        contactsBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonContacts();
            }
        });
        toolBar.add(contactsBtn);
        toolBar.addSeparator();

        JButton callLogsBtn = new JButton(BUNDLE.getString("Call-logs")); //$NON-NLS-1$
        callLogsBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonCallLogs();
            }
        });
        toolBar.add(callLogsBtn);
        toolBar.addSeparator();


        JButton smsBtn = new JButton(BUNDLE.getString("SMS")); //$NON-NLS-1$
        smsBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                fireButtonSMS();
            }
        });
        toolBar.add(smsBtn);
        toolBar.addSeparator();


        JButton callMonitorBtn = new JButton(BUNDLE.getString("Call-monitor")); //$NON-NLS-1$
        callMonitorBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonMonitorCall();
            }
        });
        toolBar.add(callMonitorBtn);
        toolBar.addSeparator();

        JButton mntmSmsMonitor = new JButton(BUNDLE.getString("SMS-monitor")); //$NON-NLS-1$
        mntmSmsMonitor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireButtonMonitorSMS();
            }
        });
        toolBar.add(mntmSmsMonitor);
        toolBar.addSeparator();

        setBorder(new EmptyBorder(5, 5, 5, 5));

        setLayout(new BorderLayout());
        add(toolBar, BorderLayout.NORTH);

//        JSplitPane splitPane = new JSplitPane();
//        splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
//
//
//        splitPane.setRightComponent(scrollPane);

        //    userLogPanel = new ColorPane();

        //JTextArea textArea = new JTextArea();
        //scrollPane.setViewportView(textArea);

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        //  splitPane.setLeftComponent(tabbedPane);
        add(tabbedPane, BorderLayout.CENTER);
        homePanel = new HomePanel(this);
        tabbedPane.addTab(BUNDLE.getString("Home"), null, homePanel, null);

        IAndroidDevice device = AdbGUI.DEVICE_MAP.get(this.imei);

        PoseidonConsole   console = new PoseidonConsole();
        EmulatorlCommandHandler handler=new EmulatorlCommandHandler();
        console.addCommandListener(handler);
        handler.setConsole(console);
        tabbedPane.add("Adb Shell",console);

        //tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        //contentPane.add(tabbedPane);
        //splitPane.add(tabbedPane);

        //  addWindowListener(this);

    }


    public String getImei() {
        return imei;
    }

    public AdbGUI getGUI() {
        return gui;
    }

    //    public void logTxt(long date, String txt) {
//        userLogPanel.append(Color.black, (new Date(date) + " " + txt + "\n"));
//    }
//
//    public void errLogTxt(long date, String txt) {
//        userLogPanel.append(Color.red, (new Date(date) + " " + txt + "\n"));
//    }
    public static final Map<String, String> PROP = new HashMap<String, String>();

    static {
        PROP.put("ro.build.id", "build的标识,一般在编译时产生");
        PROP.put("ro.build.version.sdk", "系统编译时,使用的SDK的版本");
        PROP.put("ro.build.version.sdk", "系统编译时,使用的SDK的版本");

        PROP.put("ro.build.version.codename", "版本编码名称");

        PROP.put("ro.build.version.release", "公布的版本,显示为手机信息的系统版本");

        PROP.put("ro.build.date", "系统编译的时间");

        PROP.put("ro.build.type", "系统编译类型");

        PROP.put("ro.build.user", "系统用户名");

        PROP.put("ro.build.host", "系统主机名");

        PROP.put("ro.build.tags=", "系统标记");

        PROP.put("ro.product.name", "机器名");

        PROP.put("ro.product.device", "设备名");

        PROP.put("ro.product.board", "主板名");

        PROP.put("ro.product.locale.language", "系统语言");

        PROP.put("ro.product.locale.region", "系统所在地区");

        PROP.put("net.bt.name", "蓝牙网络中显示的名称");

        PROP.put("ro.media.enc.jpeg.quality", "相机照片压缩质量,此处为100%高质量");

        PROP.put("ro.media.dec.jpeg.memcap", "相机捕捉像素,此处为800万像素");

        PROP.put("dalvik.vm.heapsize", "dalvik的虚拟内存大小");

        PROP.put("debug.sf.hw", "硬件GPU加速,1为开启,0为关闭");

        PROP.put("persist.adb.notify", "USB插入时的特别通知,1为显示,0为关闭");

        PROP.put("video.accelerate.hw", "视频硬件加速,1为开启,0为关闭");

        PROP.put("debug.sf.nobootanimation", "不显示开机动画,1为关闭动画,0为开启动画");

        PROP.put("view.touch_slop", "触摸屏灵敏度，数值越大越灵敏");

        PROP.put("view.minimum_fling_velocity", "滑动速度");

        PROP.put("view.scroll_friction", "滑动误差");

        PROP.put("wifi.interface=", "WIFI界面");

        PROP.put("wifi.supplicant_scan_interval=", "WIFI扫描间隔时间，这里值是45秒，把这个值设置越大越省电");
    }


}
